package com.jzo2o.foundations.service.impl;


import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.dto.response.ServeAggregationSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeAggregationTypeSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeCategoryResDTO;
import com.jzo2o.foundations.model.dto.response.ServeSimpleResDTO;
import com.jzo2o.foundations.service.HomeService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 首页服务列表
 *
 * @Author mr.wu
 * @Date 2025/1/19 21:27
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Resource
    private ServeMapper serveMapper;

    /**
     * 根据区域ID查询首页服务列表（限制2个服务类型，每个服务分类对应4个服务项）
     *
     * 缓存需求：
     * 1. 当返回值有数据时，缓存永久
     * 2. 当返回值无数据时，缓存30分钟（避免缓存穿透）
     *
     * @param regionId
     * @return
     */
    //@Cacheable(value = RedisConstants.CacheName.SERVE_ICON, key = "#regionId", cacheManager = RedisConstants.CacheManager.FOREVER)
    @Caching(cacheable = {
            //当返回值有数据时，缓存永久
            @Cacheable(value = RedisConstants.CacheName.SERVE_ICON, key = "#regionId", unless = "#result.size()==0", cacheManager = RedisConstants.CacheManager.FOREVER),
            //当返回值无数据时，缓存30分钟（避免缓存穿透）
            @Cacheable(value = RedisConstants.CacheName.SERVE_ICON, key = "#regionId", unless = "#result.size()>0", cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES)
    })
    @Override
    public List<ServeCategoryResDTO> firstPageServeList(Long regionId) {
        //2.根据城市编码查询所有服务类型和服务列表
        //获取服务列表
        List<ServeCategoryResDTO> serveCategoryResDTOList = serveMapper.firstPageServeList(regionId);
        //如果服务列表为空，直接返回空集合
        if(CollUtils.isEmpty(serveCategoryResDTOList)){
            return CollUtils.emptyList();
        }

        //3.服务类型取前两个，每个类型下服务项取前4个
        int size = serveCategoryResDTOList.size() >=2 ? 2 : serveCategoryResDTOList.size();
        // 获取前2个服务类型
        List<ServeCategoryResDTO> collect = serveCategoryResDTOList.stream().limit(size).collect(Collectors.toList());
        //循环获取服务类型下的4个服务项
        for (ServeCategoryResDTO cate : collect){
            //获取服务类型下的服务项图标
            List<ServeSimpleResDTO> serveResDTOList = cate.getServeResDTOList();
            //如果图标不为空的时候，取四个服务项
            if (CollUtils.isNotEmpty(serveResDTOList)){
                //判断服务项图标是否大于4个，大于4个取前4个
                int size1 = serveResDTOList.size() >=4 ? 4 : serveResDTOList.size();
                //获取前4个服务项
                List<ServeSimpleResDTO> collect1 = serveResDTOList.stream().limit(size1).collect(Collectors.toList());
                //设置服务项图标
                cate.setServeResDTOList(collect1);
            }
        }
        return collect;

    }

    /**
     * 服务类型列表展示
     * @param regionId
     * @return
     */
    @Caching(cacheable = {
            //当返回值有数据时，缓存永久
            @Cacheable(value = RedisConstants.CacheName.SERVE_ICON, key = "#regionId", unless = "#result.size()==0", cacheManager = RedisConstants.CacheManager.FOREVER),
            //当返回值无数据时，缓存30分钟（避免缓存穿透）
            @Cacheable(value = RedisConstants.CacheName.SERVE_ICON, key = "#regionId", unless = "#result.size()>0", cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES)
    })
    @Override
    public List<ServeAggregationTypeSimpleResDTO> serveTypeList(Long regionId) {
        return serveMapper.serveTypeList(regionId);
    }

    /**
     * 热门服务列表展示,当服务类型为热门服务且是上架状态，展示
     * @param regionId
     * @return
     */
    @Override
    public List<ServeAggregationSimpleResDTO> hotServeList(Long regionId) {
        //获取热门服务列表
        List<ServeAggregationSimpleResDTO> serveAggregationSimpleResDTOList = serveMapper.hotServeList(regionId);
        if(CollUtils.isEmpty(serveAggregationSimpleResDTOList)){
            return CollUtils.emptyList();
        }
        return serveAggregationSimpleResDTOList;
    }

    @Override
    public List<ServeAggregationSimpleResDTO> getById(Long id) {
        return serveMapper.getById(id);
    }
}
